package com.m3839.sdk.anti.core;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.event.SubjectBus;
import com.m3839.sdk.common.util.LogUtils;

/**
 * @author ChenBaoyang
 * @description: 距离防沉迷政策可玩时间的心跳倒计时
 * @date 2021/10/11 15:55
 */
public class AntiBeforeHeartManager {


    /**
     * 倒计时的剩余时间
     */
    private int beforeStart = -1;

    private BeforeStartThread startThread;


    private AntiBeforeHeartManager() {
    }

    private static class SingletonHolder {
        private static final AntiBeforeHeartManager INSTANCE = new AntiBeforeHeartManager();
    }

    /**
     * 返回HykbAntiBeforeHeartManager对象
     */
    public static AntiBeforeHeartManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 停止距离可玩时长的心跳
     */
    public void stopHeart() {
        if(startThread != null) {
            startThread.stopBefore();
        }
    }

    /**
     * 开始距离可玩时长的心跳
     */
    public void startBeforeHeart() {
        if(startThread == null) {
            startThread = new BeforeStartThread();
        }
        if(!startThread.isAlive()){
            startThread.start();
        }
    }

    public void setBeforeStart(int beforeStart) {
        this.beforeStart = beforeStart;
    }


    /**
     * 用来做倒计时心跳
     */
    class BeforeStartThread extends Thread {

        private static final int EVENT_BEFORE_INTERVAL = 0x0013;

        private static final long COUNT_BEFORE_INTERVAL = 1000L;

        private Handler handler;

        @Override
        public void run() {
            Looper.prepare();
            handler = new BeforeStartHandler(Looper.myLooper());
            //开始执行倒计时操作
            handler.sendEmptyMessageDelayed(EVENT_BEFORE_INTERVAL, COUNT_BEFORE_INTERVAL);
            Looper.loop();
        }

        class BeforeStartHandler extends Handler {

            public BeforeStartHandler(Looper looper) {
                super(looper);
            }

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (EVENT_BEFORE_INTERVAL == msg.what) {
                    doBefore();
                }
            }
        }

        public void doBefore() {
            LogUtils.i("m3839", "beforeStart = " + beforeStart);
            if (beforeStart > 0) {
                // 每一秒的回调，为了用于弹窗的倒计时实时更新显示,
                // beforeStart == UNIT_HOUR：表示剩余一个小时，做再一次请求
                SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_BEFORE, beforeStart);
                //倒计时长的递减
                beforeStart--;
            }

            // 加这个过滤code 如果code是200的话，就继续保持心跳
            LogUtils.i("m3839", "beforeStart = " + beforeStart);
            if (beforeStart <= 0) {
                //倒计时结束，心跳回调, 请求服务端
                SubjectBus.get().postValue(Constant.HYKB_EVENT_ANTI_BEFORE, beforeStart);
            } else {
                handler.sendEmptyMessageDelayed(EVENT_BEFORE_INTERVAL, COUNT_BEFORE_INTERVAL);
            }
        }

        /**
         * 结束倒计时
         */
        public void stopBefore() {
            handler.removeMessages(EVENT_BEFORE_INTERVAL);
        }
    }
}
